class Solution:
    def addDigits(self, num: int) -> int:
        while num > 9:
            mid_num = 0
            while num:
                mid_num += num % 10
                num //= 10
            num = mid_num
        return num


a = Solution()
print(a.addDigits(38))